<template lang="">
    <div style='    height: 100%;
    text-align: center;
    margin-top: 20%;font-size: 23px;'>
        跳转中...请耐心等待
    </div>
</template>
<script>
import auth from '../../api/auth'
import axios from 'axios'
import { mapActions } from 'vuex'
export default {
    data() {
        return {

        }
    },
    mounted() {
        let code = this.GetRequest().code
        this.getToken(code)
    },
    methods: {
        ...mapActions(['githubLogin']),
        GetRequest() {
            var url = location.search; //获取url中"?"符后的字串   
            var theRequest = new Object();
            if (url.indexOf("?") != -1) {
                var str = url.substr(1);
                var strs = str.split("&");
                for (var i = 0; i < strs.length; i++) {
                    theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
                }
            }
            return theRequest;
        },
        async getToken(code) {
            try {
                let res = await auth.getToken(code)
                let str = JSON.parse(JSON.stringify(res.data))
                let token = JSON.parse(str).access_token
                this.getUser(token)
            } catch (res) {
                console.log(res)
            }
        },
        async getUser(token) {
            try {
                let res = await auth.getUser(token)
                let str = JSON.parse(JSON.stringify(res.data))
                let userInfo = JSON.parse(str)
                this.githubLogin({ name: userInfo.login, password: userInfo.node_id, headImg:userInfo.avatar_url }).then(res => {
                    console.log(res)
                    if (res) {
                        console.log(this.$route);
                        if (this.$route.query.redirect) {
                            this.$router.replace({ path: this.$route.query.redirect });
                        } else {
                            this.$router.push({ path: "/" });
                        }
                    } else {
                        this.$message.error({ message: res.errorMsg });
                    }
                })
            } catch (res) {
            }
        },


    },
}
</script>

